package com.example.keen1;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class RegActivity extends AppCompatActivity implements View.OnClickListener {
    EditText nameEt,pwdEt,repwdEt;
    RadioGroup rg;
    RadioButton manRb,womanRb;
    CheckBox cb1,cb2,cb3,cb4;
    Button btn;

    private void initView(){
        //2.找到布局当中的控件，进行初始化
        nameEt = findViewById(R.id.et_name);
        pwdEt = findViewById(R.id.et_pwd);
        repwdEt = findViewById(R.id.et_repwd);
        rg = findViewById(R.id.rg);
        manRb = findViewById(R.id.rb_man);
        womanRb = findViewById(R.id.rb_woman);
        cb1 = findViewById(R.id.cb1);
        cb2 = findViewById(R.id.cb2);
        cb3 = findViewById(R.id.cb3);
        cb4 = findViewById(R.id.cb4);
        btn = findViewById(R.id.btn_reg);
        btn.setOnClickListener(this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    @Override
    public void onClick(View v) {
        String name = nameEt.getText().toString();
        String pwd = pwdEt.getText().toString();
        String repwd=repwdEt.getText().toString();
        if (TextUtils.isDigitsOnly(name)||TextUtils.isEmpty(pwd)||TextUtils.isEmpty(repwd)){
            Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
            return;
        }
        if (!pwd.equals(repwd)) {
            Toast.makeText(this,"两次输入密码不一致!",Toast.LENGTH_SHORT).show();
            return;
        }
        int checkedRadioButtonId=rg.getCheckedRadioButtonId();
        String gender = "男";
        if (checkedRadioButtonId == R.id.rb_man) {
            gender = "男";
        }else if (checkedRadioButtonId == R.id.rb_woman){
            gender = "女";
        }
        List<String>starList = new ArrayList<>();
        selectCbText(starList,cb1);
        selectCbText(starList,cb2);
        selectCbText(starList,cb3);
        selectCbText(starList,cb4);
        String msg = "用户名:"+name+",密码:"+pwd+",性别:"+gender+",喜欢明星:"+starList;
        Log.i("naimee","用户名");
    }
    private void selectCbText(List<String> starList, CheckBox cb1) {
        if (cb1.isChecked()) {
            starList.add(cb1.getText().toString());
        } else {
            starList.remove(cb1.getText().toString());
        }
    }
}
